require("Waoo")

local hisDB = {}
for i, v in pairs(sChatMsg) do
	if v and v.unxTime then
		table.insert(hisDB, v)
	end
end

function sort_cmp(it1, it2)
    assert(type(it1.unxTime) == "number")
    assert(type(it2.unxTime) == "number")
    return it1.unxTime < it2.unxTime
end

local fn = "him.html"
table.sort(hisDB, sort_cmp)
f = io.open(fn, "wb")
local hdr = [[
<!DOCTYPE html><html><head>
<meta charset="utf-8">
<title>waChatLog gen@]] .. os.date() .. [[</title>
<style>
.msgTime{text-align:center;color:#806000;}
.msgType_SAY{color:#FFFFFF;}
.msgType_WHISPER{color:Magenta;}
.msgType_WHISPER_INFORM{color:Magenta;}
.msgType_BN_WHISPER{color:Cyan;}
.msgType_BN_WHISPER_INFORM{color:Cyan;}
.msgType_GUILD{color:green;}
.msgType_BF_WORLD{color:BurlyWood ;}
.sender_WARLOCK{color:blue;}
.sender_PRIEST{color:blue;}
.sender_PALADIN{color:blue;}
.sender_MAGE{color:blue;}
.sender_ROGUE{color:blue;}
.sender_DRUID{color:blue;}
.sender_SHAMAN{color:blue;}
.sender_WARRIOR{color:blue;}
.sender_DEATHKNIGHT{color:blue;}
.sender_MONK{color:blue;}
.sender_DEMONHUNTER{color:blue;}
.sender_NIL{color:MidnightBlue;}
</style></head><body style="background-color:#000000;">
]]

local tail = [[
</body></html>
]]
f:write(hdr)

local msgTypeMap = {
    ["SAY"] = "说",
    ["WHISPER"] = "密",
    ["WHISPER_INFORM"] = "←密",
	["BN_WHISPER"] = "战网",	
	["BN_WHISPER_INFORM"] = "←战网",	
    ["BF_WORLD"] = "世",
    ["GUILD"] = "公"
}
for _, v in pairs(hisDB) do
	if v.sender or v.msg then
		local fmtT = os.date("%Y-%m-%d %H:%M:%S", v.unxTime)
		local str = "<p><span class=\"msgTime\">" .. fmtT .. "  </span>"
		str = str .. "<span class=\"sender_" .. (v.senderPro or "NIL") .. "\">" .. (v.senderSx or "?") .. (v.senderRace or "?") .. (v.senderPro or "?") .. "[" .. (v.sender or "NIL") .. "]</span>"
		
		local mT = msgTypeMap[v.msgType] or v.msgType
		str = str .. "<span class=\"msgType_" .. v.msgType .. "\">[" .. mT .. "]    "    
		str = str .. (v.msg or "NIL").. "</span></p>\r\n"
		--print(str)
		f:write(str)
	end
end
f:write(tail)
f:close()
print("generate chat log done.\r\nfilename:" .. fn)